<script>
import store from './store.js'
export default {
  name: 'upload',
  mixins: [store],
  data() {
    return {
      fileList: [],
      formObj: {},
      uploadFileUrl: '/v1/file/normalUpload'
    }
  },
  methods: {
    // 响应文件上传成功
    handleUploadSuccess(file, fileList, ref) {
      this.$refs[ref].clearFiles()
      this.$refs[ref].fileList.splice(0)
      this.$refs[ref].fileList.push({
        url: file.url,
        name: file.name
      })
    },
    // 得到文件对象
    handleGetFile(obj) {
      const file = obj.file
      // FormData对象
      const formObj = new FormData()
      // 文件对象
      formObj.append("file", file)
      this.formObj = formObj
      this.imgUploaded = 'success'
    },
    handleFileUpload(type, fileId = '', filePath = '') {
      console.log('loading')
      return new Promise((resolve, reject) => {
        this.uploadFile(this.formObj).then(({ data }) => {
          if (!data) {
            return
          }
          const { success } = data
          if (success) {
            console.log('success')
            if (fileId && filePath) {
              this.$set(this.formPanel[type].model, fileId, data.data.fileId)
              this.$set(this.formPanel[type].model, filePath, data.data.fileUrl)
            }
            if (fileId) {
              this.$set(this.formPanel[type].model, fileId, data.data.fileId)
            }
            if (filePath) {
              this.$set(this.formPanel[type].model, filePath, data.data.fileUrl)
            }
            resolve(data)
          } else {
            this.$message({
              type: "error",
              message: '保存失败'
            })
            reject(new Error('保存失败'))
          }
        })
      })
    }
  }
}
</script>

<style>

</style>
